<?php
/**
 * Created by PhpStorm.
 * User: td
 * Date: 15/1/9
 * Time: 上午1:49
 */

namespace Home\Model;
use Think\Model;


class UserExtModel extends Model{

    protected $tableName = 'user_ext';
    protected $pk         = 'user_ext_id';

    public function getYInfo($jobs,$duration,$keyword,$page,$rows){

        //根据查询条件在user_ext表中获取符合条件用户user_id及基本信息
        $user_ext_field = array('user_ext.user_id','degree_year','degree_school','degree_major','trust','level_y','jobs');
        $user_ext_order = '`totop` desc,`level_y` desc,`update_time` desc ';
        $job_arr = explode(",",$jobs);
        for($i=0;$i < count($job_arr);$i++){
            $job_arr[$i] = '%'.$job_arr[$i].'%';
        }
        $duration_arr = explode(",",$duration);
        for($i=0;$i < count($duration_arr);$i++){
            $duration_arr[$i] = '%'.$duration_arr[$i].'%';
        }
        if( null == $jobs){

        }else{
            $user_ext_map['jobs'] = array('like',$job_arr,'or');
        }
        if( null == $duration){

        }else{
            $user_ext_map['duration'] = array('like',$duration_arr,'or');
        }
        if( null == $keyword){

        }else{
            //关键字查询
            $user_ext_map_sub['jobs'] = array('like','%'.$keyword.'%');
            $user_ext_map_sub['degree_school'] = array('like','%'.$keyword.'%');
            $user_ext_map_sub['degree_major'] = array('like','%'.$keyword.'%');
            //$user_ext_map_sub['agent_j'] = array('like','%'.$keyword.'%');
            //$user_ext_map_sub['agent_j_desc'] = array('like','%'.$keyword.'%');
            $user_ext_map_sub['user_name'] = array('like','%'.$keyword.'%');

            $user_ext_map_sub['_logic'] = 'or';
            $user_ext_map['_complex'] = $user_ext_map_sub;
        }

        //dump($user_ext_map);
        $user_ext_result = $this->join('user on user_ext.user_id = user.user_id ','LEFT')->field($user_ext_field)->order($user_ext_order)->where($user_ext_map)->limit($page*$rows, $rows)->select();
        //dump($user_ext_result);
        if(is_array($user_ext_result)){
            foreach($user_ext_result as &$user_ext_result_one){
                //根据user_id在user表中获取status=1的user_name
                $user_field = array('user_name','status','image_url');
                $user_map['user_id'] = array('eq',$user_ext_result_one['user_id']);
                $user_map['status'] = array('neq',2);
                $user_db = M('User');
                $user_info = $user_db->field($user_field)->where($user_map)->select();
                if(is_array($user_info)){
                    $user_ext_result_one['user_name'] = $user_info[0]['user_name'];
                    $user_ext_result_one['status'] = $user_info[0]['status'];
                    $user_ext_result_one['image_url'] = $user_info[0]['image_url'];
                }else{
                    //user表中获取信息失败
                }
                //根据user_id在resume表中获取resume_id
                $resume_field = array('resume_id');
                $resume_map['user_id'] = array('eq',$user_ext_result_one['user_id']);
                $resume_db = M('Resume');
                $resume_info = $resume_db->where($resume_map)->field($resume_field)->select();
                if(is_array($resume_info)){
                    $user_ext_result_one['resume_id'] = $resume_info[0]['resume_id'];
                }else{
                    //resume表获取信息失败
                }
                //根据resume_id在experience表中获取project和role
                $experience_field = array('project_name','role');
                $experience_order = '`r_order` asc,`update_time` desc';
                $experience_map['resume_id'] = array('eq',$user_ext_result_one['resume_id']);
                $experience_db = M("Experience");
                $experience_info = $experience_db->field($experience_field)->order($experience_order)->where($experience_map)->select();
                if(is_array($experience_info)){
                    $user_ext_result_one['experiences'] = $experience_info[0];
                }else{
                    //experience表获取信息失败
                }
            }
            //排除status不等于1的用户
            $user_ext_result_return = array();
            $signal = 0;
            for($i = 0 ; $i < count($user_ext_result) ; $i++ ){
                if( 1 == $user_ext_result[$i]['status'] || (3 == $user_ext_result[$i]['status'] && $keyword == $user_ext_result[$i]['user_name'] )){
                    $user_ext_result_return[$signal] = $user_ext_result[$i];
                    $signal += 1;
                }
            }

            //排除登陆者本人
            $point = 0;
            for( $j = 0 ; $j < count($user_ext_result_return) ; $j++ ){
                if( $_SESSION['loginUser']['user_id'] != $user_ext_result_return[$j]['user_id'] ){
                    $user_ext_result_return_last[$point] = $user_ext_result_return[$j];
                    $point += 1 ;
                }
            }

            return $user_ext_result_return_last;
        }else{
            // user_ext表获取信息失败
            return 0;
        }

    }


    //PC端分页显示职位
    public function getYInfoPC($jobs,$duration,$page,$row,$keyword){





        //根据查询条件在user_ext表中获取符合条件用户user_id及基本信息
        $user_ext_field = array('user_ext.user_id','degree_year','degree_school','degree_major','trust','level_y','jobs');
        $user_ext_order = '`level_y` desc,`update_time` desc';
        $job_arr = explode(",",$jobs);
        for($i=0;$i < count($job_arr);$i++){
            $job_arr[$i] = '%'.$job_arr[$i].'%';
        }
        $duration_arr = explode(",",$duration);
        for($i=0;$i < count($duration_arr);$i++){
            $duration_arr[$i] = '%'.$duration_arr[$i].'%';
        }
        if( null == $jobs){

        }else{
            $user_ext_map['jobs'] = array('like',$job_arr,'or');
        }
        if( null == $duration){

        }else{
            $user_ext_map['duration'] = array('like',$duration_arr,'or');
        }
        if( null == $keyword){

        }else{
            //关键字查询
            $user_ext_map_sub['jobs'] = array('like','%'.$keyword.'%');
            $user_ext_map_sub['user_name'] = array('like','%'.$keyword.'%');
            $user_ext_map_sub['degree_school'] = array('like','%'.$keyword.'%');
            $user_ext_map_sub['degree_major'] = array('like','%'.$keyword.'%');
            //$user_ext_map_sub['agent_j'] = array('like','%'.$keyword.'%');
            //$user_ext_map_sub['agent_y'] = array('like','%'.$keyword.'%');
            //$user_ext_map_sub['agent_j_desc'] = array('like','%'.$keyword.'%');
            //$user_ext_map_sub['agent_y_desc'] = array('like','%'.$keyword.'%');
            //$user_ext_map_sub['agent_y_desc'] = array('like','%'.$keyword.'%');
            $user_ext_map_sub['_logic'] = 'or';
            $user_ext_map['_complex'] = $user_ext_map_sub;
        }

        //dump($user_ext_map);
        $user_ext_result = $this->join('user on user_ext.user_id = user.user_id ','LEFT')
            ->field($user_ext_field)->order($user_ext_order)->where($user_ext_map)->page($page,$row)->select();
        //dump($user_ext_result);
        if(is_array($user_ext_result)){
            foreach($user_ext_result as &$user_ext_result_one){
                //根据user_id在user表中获取status=1的user_name
                $user_field = array('user_name','status','image_url');
                $user_map['user_id'] = array('eq',$user_ext_result_one['user_id']);
                $user_map['status'] = array('neq',2);
                $user_db = M('User');
                $user_info = $user_db->field($user_field)->where($user_map)->select();
                if(is_array($user_info)){
                    $user_ext_result_one['user_name'] = $user_info[0]['user_name'];
                    $user_ext_result_one['status'] = $user_info[0]['status'];
                    $user_ext_result_one['image_url'] = $user_info[0]['image_url'];
                }else{
                    //user表中获取信息失败
                }
                //根据user_id在resume表中获取resume_id
                $resume_field = array('resume_id');
                $resume_map['user_id'] = array('eq',$user_ext_result_one['user_ext.user_id']);
                $resume_db = M('Resume');
                $resume_info = $resume_db->where($resume_map)->field($resume_field)->select();
                if(is_array($resume_info)){
                    $user_ext_result_one['resume_id'] = $resume_info[0]['resume_id'];
                }else{
                    //resume表获取信息失败
                }
                //根据resume_id在experience表中获取project和role
                $experience_field = array('project_name','role');
                $experience_order = '`update_time` desc';
                $experience_map['resume_id'] = array('eq',$user_ext_result_one['resume_id']);
                $experience_db = M("Experience");
                $experience_info = $experience_db->field($experience_field)->order($experience_order)->where($experience_map)->select();
                if(is_array($experience_info)){
                    $user_ext_result_one['experiences'] = $experience_info;
                }else{
                    //experience表获取信息失败
                }
            }
            //排除status不等于1的用户
            $user_ext_result_return = array();
            $signal = 0;
            for($i = 0 ; $i < count($user_ext_result) ; $i++ ){
                if( 1 == $user_ext_result[$i]['status'] || (3 == $user_ext_result[$i]['status'] && $keyword == $user_ext_result[$i]['user_name'] )){
                    $user_ext_result_return[$signal] = $user_ext_result[$i];
                    $signal += 1;
                }
            }

            //排除登陆者本人
            $point = 0;
            for( $j = 0 ; $j < count($user_ext_result_return) ; $j++ ){
                if( $_SESSION['loginUser']['user_id'] != $user_ext_result_return[$j]['user_id'] ){
                    $user_ext_result_return_last[$point] = $user_ext_result_return[$j];
                    $point += 1 ;
                }
            }

            return $user_ext_result_return_last;

        }else{
            // user_ext表获取信息失败
            return 0;
        }

    }

    //修改手机是否公布状态
    public function changePhoneStatus($user_id){
        $ispublic_phone_arr = $this->field(array('ispublic_phone'))->where(array('user_id'=>$user_id))->select();
        $ispublic_phone = $ispublic_phone_arr[0];
        if(1 == $ispublic_phone){
            $data['ispublic_phone'] = 3;
        }else if(2 == $ispublic_phone){
            $data['ispublic_phone'] = 4;
        }else{
            return false;
        }
        $user_ext_result = $this->save($data);
        if($user_ext_result){
            return true;
        }else{
            return false;
        }

    }

    //获取乙方基本信息扩展信息
    public function getFetcherBaseInfo($user_id){
        $user_ext_field = array('user_id','degree_year','degree_school','degree_major','ispublic_degree','ispublic_birthday','ispublic_phone','trust','level_j','level_y');
        $user_ext_map['user_id'] = array('eq',$user_id);
        $user_ext_result = $this->field($user_ext_field)->where($user_ext_map)->select();
        if(is_array($user_ext_result)){
            if($user_ext_result[0]['ispublic_degree'] == 1){
                $user_ext_result[0]['degree_year'] = '';
                $user_ext_result[0]['degree_school'] = '';
                $user_ext_result[0]['degree_major'] = '';
            }
            foreach($user_ext_result as &$user_ext_result_one){
                //获取user表信息
                $user_field = array('user_name','sex','birthday','address','image_url');
                $user_map['user_id'] = array('eq',$user_ext_result_one['user_id']);
                $user_db = M('User');
                $user_result = $user_db->field($user_field)->where($user_map)->select();
                if(is_array($user_result)){
                    $user_ext_result_one['user_name'] = $user_result[0]['user_name'];
                    $user_ext_result_one['sex'] = $user_result[0]['sex'];
                    $user_ext_result_one['image_url'] = $user_result[0]['image_url'];
                    $user_ext_result_one['address'] = $user_result[0]['address'];
                    if( 2 == $user_ext_result_one['ispublic_birthday'] ){
                        $user_ext_result_one['birthday'] = date("Y",strtotime($user_result[0]['birthday']));
                    }else{
                        $user_ext_result_one['birthday'] = '';
                    }

                    if(null == $user_result[0]['birthday'] || 0000-00-00 == $user_result[0]['birthday']){
                        $user_ext_result_one['birthday'] = '';
                    }
                }

                //获取收藏信息
                $collect_db = M('Collect');
                $collect_map['collect_type'] = 1;
                $collect_map['collecter_id'] = $_SESSION['loginUser']['user_id'];
                $collect_map['collected_id'] = $user_id;
                $collect_exist = $collect_db->where($collect_map)->select();
                if(is_array($collect_exist)){
                    $user_ext_result_one['collect_status'] = 2;
                }else{
                    $user_ext_result_one['collect_status'] = 1;
                }

                //对TA公开信息状态
                $resume_db = M('Resume');
                $resume_map['user_id'] = $_SESSION['loginUser']['user_id'];
                $resume_result = $resume_db->where($resume_map)->select();
                if(is_array($resume_result)){
                    if( 1 == $resume_result[0]['is_public'] ){
                        $user_ext_result_one['resume_public'] = 1;
                    }else{
                        $signal = 0;
                        $ispublic_one_arr = explode(",",$resume_result[0]['ispublic_one']);
                        for( $i=0 ; $i < count($ispublic_one_arr) ; $i++ ){
                            if( $user_id == $ispublic_one_arr[$i] ){
                                $signal = 1;
                            }
                        }
                        if( 1 == $signal ){
                            $user_ext_result_one['resume_public'] = 1;
                        }else{
                            $user_ext_result_one['resume_public'] = 2;
                        }
                    }
                }else{
                    $user_ext_result_one['resume_public'] = 1;
                }

            //是否自己
                if( $user_id == $_SESSION['loginUser']['user_id'] ){
                    $user_ext_result_one['is_self'] = 2;
                }else{
                    $user_ext_result_one['is_self'] = 1;
                }
            }
            return $user_ext_result[0];
        }else{
            return 0;
        }
    }

    //获取乙方个人页面甲方信息
    public function getFetcherJFInfo($user_id){
        $user_ext_field = array('agent_j','agent_j_desc','user_id');
        $user_ext_map['user_id'] = array('eq',$user_id);
        $user_ext_result = $this->field($user_ext_field)->where($user_ext_map)->select();
        if(is_array($user_ext_result)){
            foreach($user_ext_result as &$user_ext_result_one){
                $mission_field = array('mission_id','mission_name','jobs','salary1','salary2','location1','location2','duration','status');
                $mission_order = '`status` desc,`publish_time` desc';
                $mission_map['publisher_id'] = array('eq',$user_ext_result_one['user_id']);
                $mission_db = M('Mission');
                $mission_result = $mission_db->field($mission_field)->order($mission_order)->where($mission_map)->select();
                if(is_array($mission_result)){
                    $user_ext_result_one['mission'] = $mission_result;
                }else{
                    //获取mission表信息失
                }
            }
            return $user_ext_result[0];
        }else{
            return 0;
        }
    }

    //获取agent
    public function getAgent($user_id){
        $user_ext_field = array('agent_j');
        $user_ext_map['user_id'] = $user_id;
        $user_ext_result = $this->where($user_ext_map)->field($user_ext_field)->select();
        //dump($user_ext_result);
        if(is_array($user_ext_result)){
            return $user_ext_result[0]['agent_j'];
        }else{
            return '';
        }
    }

    //个人页面信息获取
    public function getSelfInfo($user_id){
        $user_ext_field = array('trust','level_j','level_y');
        $user_ext_map['user_id'] = array('eq',$user_id);
        $user_ext_result = $this->field($user_ext_field)->where($user_ext_map)->select();
        if(is_array($user_ext_result)){
            foreach($user_ext_result as &$user_ext_result_one){
                $user_field = array('user_id','user_name','image_url');
                $user_map['user_id'] = array('eq',$user_id);
                $user_db = M('User');
                $user_result = $user_db->field($user_field)->where($user_map)->select();
                if(is_array($user_result)){
                    $user_ext_result_one['user_id'] = $user_result[0]['user_id'];
                    $user_ext_result_one['user_name'] = $user_result[0]['user_name'];
                    $user_ext_result_one['image_url'] = $user_result[0]['image_url'];
                }else{
                    //获取USER表失败
                }

                //甲方积分
                $mission_db = M('Mission');
                $mission_map['publisher_id'] = array('eq',$user_id);
                $mission_map['status'] = array('eq',2);
                $mission_result = $mission_db->where($mission_map)->count();
                $user_ext_result_one['level_j_score'] = $mission_result;
                // 乙方积分
                $miss_user_relate_db = M('MissUserRelate');
                $miss_user_relate_map['follower_id'] = array('eq',$user_id) ;
                $miss_user_relate_map['is_selected'] = array('eq',3);
                //$miss_user_relate_map['evaluate'] = array('neq',null);
                $miss_user_relate_result = $miss_user_relate_db->where($miss_user_relate_map)->count();
                $user_ext_result_one['level_y_score'] = $miss_user_relate_result;
            }

            return $user_ext_result[0];
        }else{
            return false;
        }
    }

    //获取用户默认工作和时限选项
    public function getUserJobsDuration($user_id){
        $user_ext_field = array('jobs','duration');
        $user_ext_map['user_id'] = array('eq',$user_id);
        $user_ext_result = $this->field($user_ext_field)->where($user_ext_map)->select();
        if(is_array($user_ext_result)){
            return $user_ext_result[0];
        }else{
            return false;
        }
    }

    //
    public function setLevel($user_id,$level_j,$level_y){
        if( false != $level_j ){
            $data['level_j'] = $level_j;
        }
        if( false != $level_y ){
            $data['level_y'] = $level_y;
        }
        $user_ext_map['user_id']= array('eq',$user_id);
        if(is_array($data)){
            $user_ext_result = $this->where($user_ext_map)->save($data);
            if( false !== $user_ext_result){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }

    }


} 